<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use app\admin\model\HospitalLevelModel;
use app\admin\model\DrugsModel;
class DrugsController extends AdminBaseController
{
	/*
    	*药品分类列表页面
    */
	public function drugslevelcate(){
		 $levelcate=Db::name("drugs_level")->select();
		 return $this->fetch('Drugs/drugslevelcate',[
		  	     'levelcate'=>$levelcate
		 	]);
	}
	/*
		*药品分类添加页面
	*/
	public function drugsleveladd(){
		return $this->fetch('Drugs/drugsleveladd');
	}
	/*
		*药品分类添加执行页面
	*/
	public function drugsleveladddo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			$res=Db::name("drugs_level")->where('level',$data['level'])->find();
			if($res==null){
					$level = ['level'=>$data['level'],'create_time'=>time()];
				    $res1=Db::name("drugs_level")->insert($level);
					if($res1 == 1 ){
						$this->success("添加成功");
					}else{
						$this->error("添加失败");
					}
			}else{
				$this->error("该分类已存在");
			}
		
		}
	}
	/*
		*药品分类删除
	*/
	public function drugsleveldelete(){
		$data = $this->request->param();
        $res=Db::name("drugs_level")->where('id',$data['id'])->delete();
    	if($res ==1){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
	}
	/*
		*药品分类修改页面
	*/
	public function drugslevelupdate(){
		$data = $this->request->param();
		$res=Db::name("drugs_level")->where('id',$data['id'])->find();
		 return $this->fetch('Drugs/drugslevelupdate',[
		  	     'res'=>$res
		 	]);
	}
	/*
		*药品分类修改执行
	*/
	public function drugslevelupdatedo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			$res=Db::name("drugs_level")->where('id',$data['id'])->update(['level'=>$data['level']]);
			if($res == 1){
				$this->success('修改成功',url('Drugs/drugslevelcate'));
			}else{
				$this->error('修改失败',url('Drugs/drugslevelcate'));
			}
		}
	}
	/*
		*药品列表列表
	*/
	public function drugslist(){
		$res=Db::name("drugs")->select();
		return $this->fetch('Drugs/drugslist',[
				'res'=>$res
			]);
	}
	/*
		*药品添加页面
	*/
	public function drugsadd(){
		$res=Db::name("drugs_level")->select();
        $res2=Db::name("drugs_specs")->select();
		return $this->fetch('Drugs/drugsadd',[
				'res'=>$res,
                'res2'=>$res2
			]);
	}
	/*
		*药品添加执行
	*/
	public function drugsadddo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                $data['post']['more']['photos'] = [];
                foreach ($data['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                }
            }
            unset($data['photo_names']);
			$Drugs = new DrugsModel;
			$Drugs->data([
			    'personnel' => $data['personnel'],
			    'level' => $data['level'],
			    'drugs_name' => $data['drugs_name'],
			    'photo_urls' => $data['photo_urls'][0],
			    'rank'=>$data['rank'],
			    'efficiency'=>$data['efficiency'],
			    'taboo'=>$data['taboo'],
			    'drug_course'=>$data['drug_course'],
			    'aptitude'=>$data['aptitude'],
			    'status'=>$data['status'],
			    'create_time'=>time(),
			]);
			$res=$Drugs->save();
			if($res== 1){
				$this->success('添加成功',url('Drugs/drugslist'));
			}else{
				$this->error("添加失败");
			}
		}
	}
	/*
		*药品删除
	*/
	public function  drugsdelete(){
		$data = $this->request->param();
        $res=Db::name("drugs")->where('id',$data['id'])->delete();
    	if($res ==1){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
	}
	/*
		*药品修改页面
	*/
	public function drugsupdate(){
		$data = $this->request->param();
		$res=Db::name("drugs_level")->select();
		$res2=Db::name("drugs")->where("id",$data['id'])->find();
        $res3=Db::name("drugs_specs")->select();
		return $this->fetch('Drugs/drugsupdate',[
		  	     'res'=>$res,
		  	     'res2'=>$res2,
                 'res3'=>$res3
		 ]);
	}
	/*
		*药品修改执行
	*/
	public function drugsupdatedo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                $data['post']['more']['photos'] = [];
                foreach ($data['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                }
            }
            unset($data['photo_names']);
			$Drugs = new DrugsModel;
			$res=$Drugs->save([
			     'personnel' => $data['personnel'],
			    'level' => $data['level'],
			    'drugs_name' => $data['drugs_name'],
			    'photo_urls' => $data['photo_urls'][0],
			    'rank'=>$data['rank'],
			    'efficiency'=>$data['efficiency'],
			    'taboo'=>$data['taboo'],
			    'drug_course'=>$data['drug_course'],
			    'aptitude'=>$data['aptitude'],
			    'status'=>$data['status'],
			    'create_time'=>time(),
			],['id'=>$data['id']]);	
			if($res == 1){
				$this->success('修改成功',url('Drugs/drugslist'));
			}else{
				$this->error('修改失败',url('Drugs/drugslist'));
			}
		}
	}
	/*
	 * 药品规格
	 * */
	public function drugspecs(){
        $specs=Db::name("drugs_specs")->select();
        return $this->fetch('Drugs/drugspecs',[
            'specs'=>$specs
        ]);
    }
    /*
     * 药品规格添加
     * */
    public function drugspecsadd(){

        return $this->fetch('Drugs/drugspecsadd');
    }
    /*
     * 药品规格添加执行
     * */
    public function drugspecsadddo(){
        if($this->request->isPost()){
            $data=$this->request->param();
            $res=Db::name("drugs_specs")->where('specs',$data['specs'])->find();
            if($res==null){
                $level = ['specs'=>$data['specs'],'create_time'=>time()];
                $res1=Db::name("drugs_specs")->insert($level);
                if($res1 == 1 ){
                    $this->success("添加成功");
                }else{
                    $this->error("添加失败");
                }
            }else{
                $this->error("该分类已存在");
            }

        }
    }
    /*
     * 药品规格删除
     * */
    public function drugspecsdelete(){
        $data = $this->request->param();
        $res=Db::name("drugs_specs")->where('id',$data['id'])->delete();
        if($res ==1){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /*
     * 药品规格修改
     * */
    public function  drugspecsupdate(){
        $data = $this->request->param();
        $res=Db::name("drugs_specs")->where('id',$data['id'])->find();
        return $this->fetch('Drugs/drugspecsupdate',[
            'res'=>$res
        ]);
    }
    /*
     * 药品规格修改执行
     * */
    public function drugsspecsupdatedo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $res=Db::name("drugs_specs")->where('id',$data['id'])->update(['specs'=>$data['specs']]);
            if($res == 1){
                $this->success('修改成功',url('Drugs/drugspecs'));
            }else{
                $this->error('修改失败',url('Drugs/drugspecs'));
            }
        }
    }
 }

